package com.itheima.controller.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.Module;
import com.itheima.service.ModuleService;
import com.itheima.service.impl.ModuleServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/moduleServlet")
public class ModuleServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        String method = request.getParameter("method");
        Map resultMap = null;
        if ("findAll".equals(method)){
            resultMap = this.findAll(request,response);
        }else if ("add".equals(method)){
            resultMap = this.add(request,response);
        }else if ("update".equals(method)){
            resultMap = this.update(request,response);
        }else if ("delete".equals(method)){
            resultMap = this.delete(request,response);
        }

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(resultMap);
        response.getWriter().write(json);
    }

    private ModuleService service = new ModuleServiceImpl();

    private Map delete(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            String id = request.getParameter("id");
            service.delete(id);
            resultMap.put("flag",true);
            resultMap.put("message","删除成功");
        } catch (Exception e) {
            resultMap.put("flag",false);
            resultMap.put("message","删除失败!");
            e.printStackTrace();
        }
        return resultMap;
    }

    private Map update(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            Map<String, String[]> map = request.getParameterMap();
            Module module = new Module();
            BeanUtils.populate(module,map);
            service.update(module);
            resultMap.put("flag",true);
            resultMap.put("message","修改成功");
        } catch (Exception e) {
            resultMap.put("flag",false);
            resultMap.put("message","修改失败!");
            e.printStackTrace();
        }
        return resultMap;
    }

    private Map add(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            Map<String, String[]> map = request.getParameterMap();
            Module module = new Module();
            BeanUtils.populate(module,map);
            service.add(module);
            resultMap.put("flag",true);
            resultMap.put("message","添加成功");
        } catch (Exception e) {
            resultMap.put("flag",false);
            resultMap.put("message","添加失败!");
            e.printStackTrace();
        }
        return resultMap;
    }

    private Map findAll(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            List<Module> list = service.findAll();
            resultMap.put("flag",true);
            resultMap.put("resultData",list);
        } catch (Exception e) {
            resultMap.put("flag",false);
            resultMap.put("message","查询失败!");
            e.printStackTrace();
        }

        return resultMap;
    }
}
